package com.blank;

import com.alibaba.fastjson.JSONObject;
import com.blank.pojo.bo.Book;
import com.blank.service.BookService;
import com.blank.service.Impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @author BLANK
 * @date 2020/11/19
 */
@WebServlet( "/BookServlet")
public class BookServlet extends HttpServlet {
    private BookService bookService =new BookServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String option = request.getParameter("op");
        switch (option) {
            case "selectAllBook":
                try {
                    selectAllBook(response);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                break;

        }
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {




        String option = request.getParameter("op");
        switch (option) {
            case "selectAllBook":
                try {
                    selectAllBook(response);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                break;

        }
    }

    private void selectAllBook(HttpServletResponse response) throws SQLException, IOException {
        List<Book> list = bookService.selectAllBook();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", list.size());
        map.put("data", list);
        String result = JSONObject.toJSONString(map);
        response.getWriter().write(result);
    }
}
